package com.qgzj.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qgzj.common.utils.RandomIDUtils;
import com.qgzj.common.vo.MaterialRequest;
import com.qgzj.common.vo.VolumeRequest;
import com.qgzj.entity.TeachingMaterial;
import com.qgzj.entity.Volume;
import com.qgzj.mapper.VolumeMapper;
import com.qgzj.response.PageQueryRes;
import com.qgzj.util.ApplicationUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class VolumeService {
    private static Logger logger = LoggerFactory.getLogger(VolumeService.class);

    @Resource
    VolumeMapper volumeMapper;

    public List<Volume> selectByMaterialNo(String materialNo) {
        List<Volume> list = volumeMapper.selectList(new EntityWrapper<Volume>().eq("material_no", materialNo));
        return list;
    }

    public PageQueryRes<Volume> selectVolume(VolumeRequest volumeReq) {
        int page = volumeReq.getPage();
        int pageSize = volumeReq.getPageSize();
        PageQueryRes res = new PageQueryRes();
        res.setPage(page);
        res.setPageSize(pageSize);
        String materialNo = volumeReq.getMaterialNo();
        EntityWrapper<Volume> wrapper = new EntityWrapper<>();
        wrapper.eq("material_no", materialNo);
        wrapper.orderBy("id",false);
        List<Volume> volumes = volumeMapper.selectPage(new Page<>(page, pageSize),wrapper);
        res.setCount(volumeMapper.selectCount(wrapper));
        res.setList(volumes);
        return res;
    }

    public void insertVolume(VolumeRequest volumeReq){
        Volume volume = transVolumeReq2Volume(volumeReq);
        volume.setCreateTime(new Date());
        volume.setUpdateTime(new Date());
        if(StringUtils.isBlank(volume.getVolumeNo())){
            volume.setVolumeNo(ApplicationUtil.getCommonNo());
        }
        Integer insert = volumeMapper.insert(volume);
    }

    public void updateVolume(VolumeRequest volumeReq){
        Volume volume = transVolumeReq2Volume(volumeReq);
        volume.setUpdateTime(new Date());
        String volumeNo = volume.getVolumeNo();
        EntityWrapper<Volume> wrapper = new EntityWrapper<>();
        wrapper.eq("volume_no", volumeNo);
        Integer update = volumeMapper.update(volume, wrapper);
    }

    public void deleteVolume(VolumeRequest volumeReq) {
        String volumeNo = volumeReq.getVolumeNo();
        EntityWrapper<Volume> wrapper = new EntityWrapper<>();
        wrapper.eq("volume_no", volumeNo);
        Integer update = volumeMapper.delete(wrapper);
    }

    public Volume transVolumeReq2Volume(VolumeRequest volumeReq){
        Volume volume = new Volume();
//        if(StringUtils.isNotBlank(volumeReq.getVolumeNo())){
//            volume.setVolumeNo(volumeReq.getVolumeNo());
//        }
//        if(StringUtils.isNotBlank(volumeReq.getVolumeName())){
//            volume.setVolumeName(volumeReq.getVolumeName());
//        }
//        if(null!=volumeReq.getPageNum()){
//            volume.setPageNum(volumeReq.getPageNum());
//        }
//        if(StringUtils.isNotBlank(volumeReq.getOrderNo())){
//            volume.setOrderNo(volumeReq.getOrderNo());
//        }
//        if(StringUtils.isNotBlank(volumeReq.getMaterialNo())){
//            volume.setMaterialNo(volumeReq.getMaterialNo());
//        }
//        volume.setCreateTime(new Date());
//        volume.setUpdateTime(new Date());
        BeanUtils.copyProperties(volumeReq, volume);
        return volume;
    }


}
